import {sysLogin} from '@/api/login'
import {getToken, removeToken, setToken} from '@/utils/auth'

const useUserStore = defineStore(
    'user',
    {
        state: () => ({
            token: getToken(),
            name: '',
            nickName: '',
            avatar: '',
            roles: [],
            permissions: [],
            userId: '',
        }),
        actions: {
            // 登录
            login(userInfo) {
                // setToken(1)
                const loginName = userInfo.loginName.trim()
                const loginPwd = userInfo.loginPwd
                return new Promise((resolve, reject) => {
                    sysLogin({loginName, loginPwd}).then(res => {
                        let resData = res.data || {};
                        setToken(resData.userId)
                        this.token = resData.userId
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取用户信息
            getInfo() {
                return new Promise((resolve, reject) => {
                
                })
            },
            // 退出系统
            logOut(t) {
                return new Promise((resolve, reject) => {
                    this.token = ''
                    this.roles = []
                    this.permissions = []
                    removeToken()
                    resolve()
                })
            }
        }
    })

export default useUserStore
